package com.cby.user.test3D;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.*;
import java.util.Arrays;

/*
**  Create by: 王孙康宏
    Date:2021-10-24
    Time:21:55
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Box implements Comparable<Box>, Serializable {
    // 货物的长宽高
    public float l,w,h;
    // 物品数量
    int count;
    // 方向约束限制 为1则表示该边可以作为物品的高度，为0表示不可以
    int[] direction;
    // 方向尺寸列表 根据方向限制和尺寸生成
    float[][] orientations;

    public Box(float l, float w, float h, int[] direction,int count) {
        this.l = l;
        this.w = w;
        this.h = h;
        this.count = count;
        this.direction = direction;
        // 根据尺寸和方向约束生成对应的尺寸
        // 1. 统计有多少个1
        int d = 0;
        for (int i : direction) {
            if(i!=0){
                d++;
            }
        }
        // 2.根据不同的1数量赋予orientations不同长度
        try {
            switch (d){
                case 0:throw new Exception("有个箱子没有一条边可以作为高");
                case 1:this.orientations = new float[2][3];break;
                case 2:this.orientations = new float[4][3];break;
                case 3:this.orientations = new float[6][3];break;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        // 3.添加数据
        int j = 0;
        for (int i = 0; i < direction.length; i++) {
            if(direction[i]==1){
                if(i==0){
                    orientations[j][2] = l;
                    orientations[j][1] = w;
                    orientations[j][0] = h;
                    j++;
                    orientations[j][2] = l;
                    orientations[j][0] = w;
                    orientations[j][1] = h;
                    j++;
                }else if(i==1){
                    orientations[j][2] = w;
                    orientations[j][1] = l;
                    orientations[j][0] = h;
                    j++;
                    orientations[j][2] = w;
                    orientations[j][0] = l;
                    orientations[j][1] = h;
                    j++;
                }else{
                    orientations[j][2] = h;
                    orientations[j][1] = l;
                    orientations[j][0] = w;
                    j++;
                    orientations[j][2] = h;
                    orientations[j][0] = l;
                    orientations[j][1] = w;
                    j++;
                }
            }
        }
        // 4.输出验证一下
//        for (int[] orientation : orientations) {
//            System.out.println(Arrays.toString(orientation));
//        }
    }

    public Box(Box box){
        l = box.getL();
        w = box.getW();
        h = box.getH();
        count = box.getCount();
        direction = box.getDirection().clone();
        orientations = box.getOrientations().clone();
    }

    public float getL() {
        return orientations[0][0];
    }

    public float getW() {
        return orientations[0][1];
    }

    public float getH() {
        return orientations[0][2];
    }

//    @Override
//    public int compareTo(Box o) {
//        // 按照底面积从大到小排序
//        float[] orientation1 = this.orientations[0];
//        float[] orientation2 = o.getOrientations()[0];
//        float s1 = orientation1[0]*orientation1[1];
//        float s2 = orientation2[0]*orientation2[1];
//        if(s2<s1){
//            return -1;
//        }else if(s2>s1){
//            return 1;
//        }else{
//            return 0;
//        }
//    }

    @Override
    public int compareTo(Box o) {
        // 按照体积从大到小排序
        float[] orientation1 = this.orientations[0];
        float[] orientation2 = o.getOrientations()[0];
        float v1 = orientation1[0]*orientation1[1]*orientation1[2];
        float v2 = orientation2[0]*orientation2[1]*orientation2[2];
        if(v2<v1){
            return -1;
        }else if(v2>v1){
            return 1;
        }else{
            return 0;
        }
    }
}
